<?php
namespace app\admin\controller;

use app\BaseController;
use think\facade\View;

class Login extends AdminBase {
    public function initialize()
    {
       if($this->isLogin()){
            return $this->redirect(url('index/index'));
            return $this->error();
       }
    }

    public function index(){
        return View::fetch();
    }


    public function check(){

        if(!$this->request->isPost()){
            return show(config("status.error"), '请求方式错误');
        }

        // 参数验证
        ##1.用户名密码验证
        $username = $this->request->param("username",'',"trim");
        $password = $this->request->param("username",'',"trim");
        $captcha = $this->request->param("captcha",'',"trim");

        if(empty($username) ||empty($password) || empty($captcha)){
            return show(config());
        }


        ##2.验证码检验
        if(!captcha_check($captcha)){  //记得开启session  --middle中开启sessioninit
            return show(config(''),'验证码不正确');
        }

        ##   判断用户名密码是否正确
        #获取用户信息
        $adminUserObj = new AdminUser();
        $adminUser = $adminUserObj->getAdminUserByUserName($username);
        $adminUser = $adminUser->toArray();

        ## 判断用户名
        if(empty($adminUser) || $adminUser->status!=1){
            return show(1000,"用户名不存在");
        }

        ## 判断密码是否正确
        if ($adminUser['password'] != md5($password."__salt__")){
            return show(1001,'密码错误');
        }

        //登录成功,记录session
        session("adminUser", $adminUser);


        return show(config("status.success"), "登录成功");
    }
}